rm(list=ls())
library(tidyverse)
library(readxl)

# Create folder to receive files
dir.create(here::here("data","processed","citycharacteristics"))

# Read and clean gdp mun from 2002 to 2017
file <- list.files(here::here("data","raw","IBGE"), pattern = "tab5938", full.names = T)
gdp_mun_5938 <- read_excel(file,
                           range = "A5:R5574",
                           col_names = c("mun_code","mun_name",seq(2002,2017)),
                           col_types = c("text","text",rep("numeric",16)),
                           na = "...") %>% 
  pivot_longer(cols = 3:18,names_to = "year") %>% 
  rename(mun_gdp = value) %>% 
  mutate(gdp_unit = "Produto Interno Bruto a preços correntes (Mil Reais)")


# Read and clean gdp mun from 1999 to 2001
file <- list.files(here::here("data","raw","IBGE"), pattern = "tab21", full.names = T)
gdp_mun_21 <- read_excel(file,
                           range = "A5:E5574",
                           col_names = c("mun_code","mun_name",seq(1999,2001)),
                           col_types = c("text","text",rep("numeric",3)),
                           na = "...") %>% 
  pivot_longer(cols = 3:5,names_to = "year") %>% 
  rename(mun_gdp = value) %>% 
  mutate(gdp_unit = "Produto Interno Bruto a preços correntes (Mil Reais)")



gdp_mun <- gdp_mun_5938 %>% 
  bind_rows(gdp_mun_21) %>% 
  arrange(mun_code,year) %>% 
  mutate(mun_code = str_sub(mun_code,1,-2),
         year = as.numeric(year))

# SaveRdS ----

write_rds(gdp_mun,here::here("data","processed","citycharacteristics","gdp_mun.rds"))


